/* 璇ユ彃浠跺疄鐜颁簡Banner鍥剧殑婊氬姩鏁堟灉銆�
 * 
 * 涓€銆佹彃浠剁殑灞炴€э細
 * images : 鎺ュ彈鏁扮粍绫诲瀷锛屾暟缁勭殑姣忎釜鍊煎簲涓哄璞°€傚璞″叿鏈夊睘鎬э細 src->鍥剧墖鐨勮矾寰� title->鍥剧墖鎸囦笂鍚庣殑鏂囧瓧   href->鐐瑰嚮鍥剧墖璺宠浆鐨勯〉闈�
 * 
 * scrollTime : 姣忓紶鍥剧墖鐨勫仠鐣欐椂闂达紝鍗曚綅姣銆� 2000
 * bannerHeight : Banner鍥剧殑楂樺害.  500px
 * 
 * iconColor : 鎻愮ず鍥炬爣鐨勯鑹层€傞粯璁� white
 * iconHoverColor : 鎻愮ず鍥炬爣鎸囦笂涔嬪悗鐨勯鑹层€�  榛樿 orange
 * iconPosition : 鎻愮ず鍥炬爣鐨勪綅缃紝鍙€夊€糽eft/center/right銆� 榛樿center
 * 
 */
!function($){
	$.fn.scrollBanner = function(obj){
		// 澹版槑鍚勪釜灞炴€х殑榛樿鍊�
		var defaults = {
			images : [],
			scrollTime : 2000,
			bannerHeight : "500px",
			iconColor : "white",
			iconHoverColor : "orange",
			iconPosition : "center"
		}
		// 灏嗛粯璁ゅ€间笌浼犲叆鐨勫璞℃瘮瀵癸紝濡傛灉浼犲叆鐨勫璞℃湁鏈祴鍊煎睘鎬э紝鍒欎娇鐢ㄩ粯璁ゅ璞＄殑灞炴€�
		obj = $.extend(defaults,obj);
		
		// 缁勪欢DOM甯冨眬
		$("body").css({"padding":"0px","margin" : "0px"});
		
		this.empty().append("<div class='scrollBanner-banner'></div>");
		$.each(obj.images,function(index,item){
			$(".scrollBanner-banner").append("<div class='scrollBanner-bannerInner'><a href='"+item.href+"' target='_black'><img src='"+item.src+"' title='"+item.title+"' /></a></div>");
		});
		$(".scrollBanner-banner").append("<div class='scrollBanner-bannerInner'><a href='"+obj.images[0].href+"' target='_black'><img src='"+obj.images[0].src+"' title='"+obj.images[0].title+"' /></a></div>");
		
		this.append("<div class='scrollBanner-icons'></div>");
		$.each(obj.images,function(index,item){
			// data-* 灞炴€ф槸H5鍏佽鐢ㄦ埛鑷鍦℉TML鏍囩涓婁繚瀛樻暟鎹殑灞炴€с€�
			// 淇濆瓨鍦╠ata-*涓殑鏁版嵁锛屽彲浠ヤ娇鐢↗S璇诲彇璋冪敤銆�
			$(".scrollBanner-icons").append("<span class='scrollBanner-icon' data-index='"+index+"'></span>");
		})
		
		// 璁剧疆鍚勭CSS
		this.css({
			"width": "100%",
			"height": obj.bannerHeight,
			"overflow": "hidden",
			"position": "relative"
		});
		
		$(".scrollBanner-banner").css({
			"width": obj.images.length+1+"00%",
			"height": obj.bannerHeight,
			"transition": "all .5s ease"
		});
		
		$(".scrollBanner-bannerInner").css({
			"width": 100/(obj.images.length+1)+"%",
			"height": obj.bannerHeight,
			"overflow": "hidden",
			"float": "left"
		});
		
		$(".scrollBanner-bannerInner img").css({
			"width": "1920px",
			"height": obj.bannerHeight,
			"position": "relative",
			"left": "50%",
			"margin-left": "-960px",
		});
		
		$(".scrollBanner-icons").css({
			"width": 30*obj.images.length+"px",
			"height": "7px",
			"position":"absolute",
			"bottom":"40px",
			"z-index": "100",
		});
		
		switch (obj.iconPosition){
			case "left":
				$(".scrollBanner-icons").css({
					"left":"40px",
				});
				break;
			case "right":
				$(".scrollBanner-icons").css({
					"right":"40px",
				});
				break;
			case "center":
				$(".scrollBanner-icons").css({
					"left":"50%",
					"margin-left": "-"+15*obj.images.length+"px"
				});
				break;
		}
		
		$(".scrollBanner-icon").css({
			"width": "20px",
			"height": "7px",
			"background-color": obj.iconColor,
			"display": "inline-block",
			"margin": "0px 5px",
		});
		$(".scrollBanner-icon:eq(0)").css({
			"background-color":obj.iconHoverColor
		});
		
		
		// 瀹炵幇Banner婊氬姩鍔熻兘
		var count = 1;
		var icons = $(".scrollBanner-icon");
		setInterval(function(){
			$(".scrollBanner-banner").css({
				"margin-left":"-"+count+"00%",
				"transition": "all .5s ease"
			});
			$(".scrollBanner-icon").css("background-color",obj.iconColor);
			$(".scrollBanner-icon:eq("+count+")").css("background-color",obj.iconHoverColor);
			
			if(count>=obj.images.length)
			$(".scrollBanner-icon:eq("+0+")").css("background-color",obj.iconHoverColor);
			
			if(count>obj.images.length){
				count = 0;
				$(".scrollBanner-banner").css({
					"margin-left":"0px",
					"transition": "none"
				});
			}
			count++;
		},obj.scrollTime);
		
		
		// 灏忓浘鏍囨寚涓婁互鍚庡彉鑹插苟涓斿垏鎹anner鍥�
		$(".scrollBanner-icon").mouseover(function(){
			$(".scrollBanner-icon").css("background-color",obj.iconColor);
			// 鈫� 鐢眘pan瑙﹀彂mouseover浜嬩欢锛屽垯this鎸囧悜杩欎釜span銆�
			// 鈫� 浣嗘槸锛岃繖this鏄疛S瀵硅薄锛屽繀椤讳娇鐢�$灏佽鎴怞Query瀵硅薄銆�
			$(this).css("background-color",obj.iconHoverColor);
			
			var index = $(this).attr("data-index");
			// 灏嗚鏁板櫒count淇敼涓篿ndex鐨勫€硷紝璁〣anner婊氬姩鐨勫畾鏃跺櫒鑳藉鍒氬ソ婊氬埌鎵€鎸囧浘鐗囩殑涓嬩竴寮�
			count  = index; 
			$(".scrollBanner-banner").css({
				"transition": "none",
				"margin-left": "-"+index+"00%"
			});
		});
		
		
	}
}(jQuery);

